package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.workflow.dao.BpmNodeRuleDao;
import com.ccp.dev.workflow.model.BpmNodeRule;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassDescribe:
 * 流程跳转规则业务逻辑层
 * @author :wangcheng
 * Date: 2019-07-08
 */
@Service
public class BpmNodeRuleService extends BaseService<BpmNodeRule> {

    @Resource
    private BpmNodeRuleDao bpmNodeRuleDao;
    /**
     * 根据流程定义id和节点id获取跳转规则列表
     * @param procDefId 流程定义id
     * @param nodeId 节点id
     */
    public List<BpmNodeRule> getByProcDefIdNodeId(String procDefId, String nodeId) {
        return bpmNodeRuleDao.getByProcDefIdNodeId(procDefId,nodeId);
    }

    /**
     * 根据流程定义id获取跳转规则列表
     * @param procDefId 流程定义id
     * @return 跳转规则列表数据
     */
    public List<BpmNodeRule> getByProcDefId(String procDefId) {
        return bpmNodeRuleDao.getByProcDefIdNodeId(procDefId,null);
    }

    /**
     * 规则重新排序
     * @param ruleIds 规则id组合字符串
     */
    @Transactional(rollbackFor = Exception.class)
    public void reSort(String ruleIds) {
        if (StringUtil.isEmpty(ruleIds)) {
            return;
        }
        String[] ruleIdAry = ruleIds.split(",");
        for (int i = 0; i < ruleIdAry.length; i++) {
            String ruleId = ruleIdAry[i];
            bpmNodeRuleDao.reSort(ruleId, (long)i);
        }
    }
}
